<div>
    <h3>High Availability</h3>
    <div *ngIf="hasStatus" class="mb-4">
        <p-treeTable [value]="status" [scrollable]="true" [tableStyle]="{ 'min-width': '50rem' }">
            <ng-template pTemplate="header">
                <tr>
                    <th></th>
                    <th>This Server</th>
                    <th *ngIf="hasPartnerColumn">Partner</th>
                </tr>
            </ng-template>
            <ng-template pTemplate="body" let-rowNode let-rowData="rowData">
                <tr [ttRow]="rowNode" [class]="rowData.styleClass">
                    <td>
                        <div class="flex align-items-center justify-content-between">
                            <div>
                                <p-treeTableToggler [rowNode]="rowNode"></p-treeTableToggler>
                                <span *ngIf="rowData.title">
                                    {{ rowData.title }}
                                </span>
                            </div>
                            <div>
                                <ng-container [ngSwitch]="rowData.title">
                                    <app-help-tip *ngSwitchCase="'Status time'" [subject]="rowData.title | lowercase">
                                        <p>
                                            This is the time when the server reported its state for the last time. it is
                                            not necessarily the time when the state information was refreshed in the UI.
                                            The presented state information is typically delayed by 10 to 30 seconds
                                            because it is cached by the Kea servers and the Stork backend. Caching
                                            minimizes the performance impact on the DHCP servers reporting their states
                                            over the control channel.
                                        </p>
                                    </app-help-tip>
                                    <app-help-tip *ngSwitchCase="'Status age'" [subject]="rowData.title | lowercase">
                                        <p>
                                            This is the duration between the "Status Time" and now, i.e. it indicates
                                            how long ago the server reported its state. A long duration indicates that
                                            there is a communication problem with the server. The typical duration is
                                            between 10 and 30 seconds.
                                        </p>
                                    </app-help-tip>
                                    <app-help-tip
                                        *ngSwitchCase="'Control status'"
                                        [subject]="rowData.title | lowercase"
                                    >
                                        <p>
                                            An <span class="font-bold">online</span> control status indicates that the
                                            server responds to the commands over the control channel.
                                        </p>
                                        <p>
                                            An <span class="font-bold">offline</span> control status indicates that it
                                            is not responding and may be down.
                                        </p>
                                        <p>
                                            Finally, an <b>unknown</b> control status may be signalled upon the server's
                                            startup, when the information about its actual status is not yet available.
                                        </p>
                                    </app-help-tip>
                                    <app-help-tip
                                        *ngSwitchCase="'Heartbeat status'"
                                        [subject]="rowData.title | lowercase"
                                        width="40vw"
                                    >
                                        <p>
                                            The heartbeat status reports whether or not the server responds to the
                                            heartbeat commands from its partner. The heartbeat commands are exchanged
                                            between the partners regularly to check if the servers remain operational
                                            and to gather their current states and act accordinly.
                                        </p>
                                        <p>
                                            An <b>ok</b> status is desired at all times. It indicates a healthy
                                            heartbeat communication with the server.
                                        </p>
                                        <p>
                                            A <span class="font-bold">failed</span> status occurs when the recent
                                            attempt to send the heartbeat to the server fails. In this case, we say that
                                            the partners are in the
                                            <span class="font-italic">communication-interrupted</span> state. They may
                                            be in this state temporarily if the communication problem is transient. If
                                            the communication is interrupted longer, it may trigger the failover
                                            procedure, resulting in the partner's transition to the
                                            <span class="font-bold">partner-down</span>
                                            state.
                                        </p>
                                        <p>
                                            An <span class="font-bold">unknown</span> status may sometimes occur when
                                            the servers haven't tried to exchange the heartbeats yet.
                                        </p>
                                    </app-help-tip>
                                    <app-help-tip
                                        *ngSwitchCase="'State'"
                                        [subject]="rowData.title | lowercase"
                                        width="40vw"
                                    >
                                        <p>The following are the possible server states:</p>
                                        <ul>
                                            <li>
                                                <span class="font-bold white-space-nowrap">hot-standby</span> - normal
                                                operation in the
                                                <span class="font-italic white-space-nowrap">hot-standby</span> mode.
                                            </li>
                                            <li>
                                                <span class="font-bold white-space-nowrap">load-balancing</span> -
                                                normal operation in the
                                                <span class="font-italic white-space-nowrap">load-balancing</span> mode.
                                            </li>
                                            <li>
                                                <span class="font-bold white-space-nowrap">passive-backup</span> - the
                                                server has no active partner, unlike in the
                                                <span class="font-italic white-space-nowrap">load-balancing</span> or
                                                <span class="font-italic white-space-nowrap">hot-standby</span> mode.
                                                This server may be configured to send lease updates to the backup
                                                servers, but there is no automatic failover triggered in case of a
                                                failure.
                                            </li>
                                            <li>
                                                <span class="font-bold">waiting</span> - the server is booting up and
                                                will try to synchronize its lease database.
                                            </li>
                                            <li>
                                                <span class="font-bold">syncing</span> - the server is synchronizing its
                                                database after a failure.
                                            </li>
                                            <li>
                                                <span class="font-bold">ready</span> - the server has synchronized its
                                                lease database and will start normal operation shortly.
                                            </li>
                                            <li>
                                                <span class="font-bold">terminated</span> - the server no longer
                                                participates in the HA setup because of too-high clock skew.
                                            </li>
                                            <li>
                                                <span class="font-bold">maintained</span> - the server is under
                                                maintenance.
                                            </li>
                                            <li>
                                                <span class="font-bold">partner-maintained</span> - the server is
                                                responding to all DHCP queries while its partner is in maintenance.
                                            </li>
                                            <li>
                                                <span class="font-bold">unavailable</span> - communication with the
                                                server failed. It may have crashed or have been shut down.
                                            </li>
                                        </ul>
                                    </app-help-tip>
                                    <app-help-tip *ngSwitchCase="'Scopes'" [subject]="rowData.title | lowercase">
                                        <p>
                                            This is a list of HA scopes currently being served by this server. If the
                                            server is responding to the DHCP queries as a
                                            <span class="font-bold">primary</span> or
                                            <span class="font-bold">secondary</span> in the
                                            <span class="font-bold white-space-nowrap">load-balancing</span> mode or as
                                            a <span class="font-bold">primary</span> in the
                                            <span class="font-bold white-space-nowrap">hot-standby</span> mode, it is
                                            typically a single scope shown. There may be two scopes shown if a
                                            <span class="font-bold white-space-nowrap">load-balancing</span> server is
                                            currently serving all DHCP clients when its partner is down. There may be no
                                            scopes shown when it is a <span class="font-bol">standby</span> server in
                                            the <span class="font-bold white-space-nowrap">hot-standby</span> mode,
                                            because such a server is not responding to any DHCP queries, but passively
                                            receiving lease updates from the <span class="font-bold">primary</span>. The
                                            <span class="font-bold">standby</span> server will start serving the
                                            <span class="font-bold">primary</span> server scope in the event of the
                                            <span class="font-bold">primary</span> failure.
                                        </p>
                                    </app-help-tip>
                                    <app-help-tip
                                        *ngSwitchCase="'Last in partner-down'"
                                        [subject]="rowData.title | lowercase"
                                    >
                                        <p>
                                            This is the last time when the server went to the
                                            <span class="font-bold white-space-nowrap">partner-down</span> state because
                                            its partner was considered offline as a result of an unexpected termination
                                            or shutdown.
                                        </p>
                                    </app-help-tip>
                                    <app-help-tip
                                        *ngSwitchCase="'Unacked clients'"
                                        [subject]="rowData.title | lowercase"
                                    >
                                        <p>
                                            This is the number of clients considered unacked by the partner. This value
                                            is only set when the partner has lost heartbeat communication with this
                                            server and has started the failover procedure, by monitoring whether the
                                            server is responding to DHCP traffic. The unacked number indicates clients
                                            that have been trying to get leases from this server longer than the time
                                            specified by the max-ack-delay configuration parameter.
                                        </p>
                                    </app-help-tip>
                                    <app-help-tip
                                        *ngSwitchCase="'Connecting clients'"
                                        [subject]="rowData.title | lowercase"
                                    >
                                        <p>
                                            This is the total number of clients trying to get new leases from the server
                                            with which the partner server is unable to communicate via
                                            <span class="font-bold white-space-nowrap">ha-heartbeat</span>. It includes
                                            both unacked clients and the clients for which the secs field or elapsed
                                            time option is below the
                                            <span class="font-bold white-space-nowrap">max-ack-delay</span>.
                                        </p>
                                    </app-help-tip>
                                    <app-help-tip
                                        *ngSwitchCase="'Analyzed packets'"
                                        [subject]="rowData.title | lowercase"
                                    >
                                        <p>
                                            This is a total number of packets directed to the server with which the
                                            partner is unable to communicate using
                                            <span class="font-bold white-space-nowrap">ha-heartbeat</span>. It may
                                            include several packets from the same client who tried to resend a
                                            DHCPDISCOVER or Solicit after the server failed to respond to the previous
                                            queries.
                                        </p>
                                    </app-help-tip>
                                    <app-help-tip
                                        *ngSwitchCase="'Failover progress'"
                                        [subject]="rowData.title | lowercase"
                                    >
                                        <p>
                                            It shows a progress bar for a failover procedure. When it hits 100% the
                                            server will consider its partner offline, and will transition to the
                                            <span class="font-bold white-space-nowrap">partner-down</span>
                                            state. The progress is calculated from the number of unacked clients
                                            relative to the maximum number of unacked clients.
                                        </p>
                                    </app-help-tip>
                                    <app-help-tip *ngSwitchCase="'Summary'" [subject]="rowData.title | lowercase">
                                        <p>It is a summary text describing the status of the HA setup.</p>
                                    </app-help-tip>
                                </ng-container>
                            </div>
                        </div>
                    </td>
                    <td *ngFor="let column of rowData.cells">
                        <span *ngIf="column.value !== undefined" class="ha-ok">{{ column.value }}</span>
                        <span *ngIf="column.appId && column.appName">
                            at
                            <app-entity-link
                                entity="app"
                                [showEntityName]="false"
                                [attrs]="{ id: column.appId, type: 'kea', name: column.appName }"
                            ></app-entity-link>
                        </span>
                        <ng-container [ngSwitch]="column.iconType">
                            <span *ngSwitchCase="'ok'" class="ml-2 pi pi-check icon-ok"></span>
                            <span *ngSwitchCase="'warn'" class="ml-2 pi pi-exclamation-triangle icon-warn"></span>
                            <span *ngSwitchCase="'error'" class="ml-2 pi pi-exclamation-circle icon-error"></span>
                            <span *ngSwitchCase="'pending'" class="ml-2 pi pi-spin pi-spinner icon-pending"></span>
                        </ng-container>
                        <p-progressBar
                            *ngIf="column.hasOwnProperty('progress')"
                            [value]="column.progress"
                        ></p-progressBar>
                    </td>
                </tr>
            </ng-template>
        </p-treeTable>
        <div class="mt-2">
            <span class="font-italic text-sm">Status refresh in {{ refreshCountdown }} s.</span>
        </div>
    </div>
    <p-progressSpinner *ngIf="!hasStatus && !loadedOnce"></p-progressSpinner>
    <div *ngIf="!hasStatus && loadedOnce">High Availability is not enabled on this server.</div>
</div>
